import router from '@system.router';
import prompt from '@system.prompt';
import pasteboard from '@ohos.pasteboard';
import { listEventType } from '../../service';

export default {
    data: {
        list: []
    },
    async onInit() {
        console.info("parentId: " + this.parentId);
        this.list = await listEventType(this.parentId);
        if (!this.title) this.title = '类型管理';
    },
    cellClick(e) {
        if (!e.detail.data) return;
        const { id: parentId, name, icon, color } = e.detail.data;
        if (!this.parentId) {
            router.push({
                uri: "pages/event-type/event-type",
                params: { parentId, title: name }
            });
            return
        }
        prompt.showActionMenu({
            title: '操作',
            buttons: [
                { text: '复制信息', color: '#1989fa', },
                { text: '修改', color: '#07c160', },
                { text: '删除', color: '#ee0a24', },
            ],
            success: function (data) {
                switch (data.tapIndex) {
                    case 0:
                        const pasteData = pasteboard.createPlainTextData(`{"i":"${icon}","n":"${name}","c":"${color}"}`);
                        const systemPasteboard = pasteboard.getSystemPasteboard();
                        systemPasteboard.setPasteData(pasteData, (error, data) => {
                            if (error) {
                                console.error('Failed to setPasteData. Cause: ' + error.message);
                                return;
                            }
                            prompt.showToast({
                                message: '复制成功！',
                                duration: 2000,
                            });
                        });
                        break;
                    default:
                        prompt.showToast({
                            message: '开发中……',
                            duration: 2000,
                        });
                        break;
                }
            },
            fail: function (data) {
                console.log('dialog fail callback' + data.errMsg);
            },
        });

    },
    back() {
        router.back()
    }
}
